package trident.partition;

import org.apache.storm.trident.operation.BaseFunction;
import org.apache.storm.trident.operation.TridentCollector;
import org.apache.storm.trident.tuple.TridentTuple;
import org.apache.storm.tuple.Fields;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Iterator;

public class PrintFunction extends BaseFunction {
    private final Logger logger = LoggerFactory.getLogger(PrintFunction.class);

    @Override
    public void execute(TridentTuple tuple, TridentCollector collector) {
        Fields fields = tuple.getFields(); // List 集合，所有的fields名称
        Iterator<String> iterator = fields.iterator();
        StringBuilder stringBuilder = new StringBuilder(); // 用于追加
        while (iterator.hasNext()) { // 迭代器遍历
            String field = iterator.next(); // 取出迭代器中的值
            stringBuilder.append(field).append(": ").append(tuple.getValueByField(field)).append(", ");
        }
        logger.info(String.valueOf(stringBuilder));
    }
}
